#include<iostream>
#include<pthread.h>
#include<chrono>
#include<vector>
#include"MyKV.cpp"
using namespace std;

int TEST_NUM;
const int THREAD_NUM = 1;
typedef void* (*PF)(void*);
MyKV<int, string>* sk;

void* writeSk(void* args) {
    for(int i = 0; i < TEST_NUM; i ++){
         sk->add(rand() % TEST_NUM, "a");
    }
    return nullptr;
}

void* readSk(void* args) {
    for(int i = 0; i < TEST_NUM; i ++) {
        sk->search(rand() % TEST_NUM);
    }
    return nullptr;
}

void workThread(PF func, string op) {
    pthread_t tid[THREAD_NUM];
    auto start = chrono::high_resolution_clock::now();
    for(int i = 0; i < THREAD_NUM; i ++) {
        if(pthread_create(&tid[i], nullptr, func, nullptr) != 0 ){
            perror("pthread_create error");
            exit(1);
        }
    }

    for(int i = 0; i < THREAD_NUM; i ++){
        if(pthread_join(tid[i], nullptr) != 0){
            perror("pthread_join error");
            exit(1);
        }
    }

    auto finish = chrono::high_resolution_clock::now();
    chrono::duration<double> elapsed = finish - start;
    cout << op << "elapsed:" << elapsed.count() << endl;
}
int main(int argc, char* argv[]) {
    srand(time(NULL));
    sk = new MyKV<int, string>();
    vector<int> v = {100000, 500000, 1000000};
    for(int i = 0; i < 3; i ++) {
        TEST_NUM = v[i];
        workThread(writeSk, "insert");
        workThread(readSk, "search");
    }
    return 0;
}
